package com.jlp.source.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.jlp.source.enums.ContextConst;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lingpeng.jiang
 * @create 2022-01-09 14:49
 * @Description //TODO 多数据源配置类
 **/
@Configuration
public class multipleDataSourcesConfig {

    @Resource
    private MysqlDataSourceProperties mysqlDataSourceProperties;

    @Resource
    private TidbDataSourceProperties tidbDataSourceProperties;

    /**
     * 创建mysql数据源并交给jat管理，实现2pc分布式事务
     * @return
     */
    @Bean("mysqlDataSource")
    public DataSource mysqlDataSource(){
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(mysqlDataSourceProperties.getUrl());
        mysqlXADataSource.setUser(mysqlDataSourceProperties.getUsername());
        mysqlXADataSource.setPassword(mysqlDataSourceProperties.getPassword());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        // 事务管理器
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("mysqlDataSource");
        return atomikosDataSourceBean;
    }

    /**
     * 创建tidb数据源
     * @return
     */
    @Bean("tidbDataSource")
    public DataSource tidbDataSource(){
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(tidbDataSourceProperties.getUrl());
        mysqlXADataSource.setUser(tidbDataSourceProperties.getUsername());
        mysqlXADataSource.setPassword(tidbDataSourceProperties.getPassword());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        // 事务管理器
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("tidbDataSource");
        return atomikosDataSourceBean;
    }


    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源 MySQL
        dynamicDataSource.setDefaultTargetDataSource(mysqlDataSource());
        // 配置多数据源
        Map<Object, Object> dataSourceMap = new HashMap<>(4);
        dataSourceMap.put(ContextConst.DataSourceType.MYSQL.name(),mysqlDataSource());
        dataSourceMap.put(ContextConst.DataSourceType.TIDB.name(),tidbDataSource());
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        return dynamicDataSource;
    }


    /**
     * 配置@Transactional注解事务
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }
}
